<script setup>
import router from '@/router'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/api/login'
import { useUserStore } from '@/stores'
const userStore = useUserStore()

const loading = ref(false)
const loginForm = ref({ username: '2025001', password: '123456', })
const loginFormRef = ref(null)
const loginRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" }
  ],
}

function handleLogin() {
  loginFormRef.value.validate(valid => {
    if (valid) {
      loading.value = true
      login(loginForm.value.username, loginForm.value.password).then(res => {
        console.log(res)
        if (res.data.code === 200) {
          ElMessage.success('登录成功')
          userStore.setToken(res.data.token)
          // 跳转到首页
          router.replace('/')
        } else {
          loading.value = false
          ElMessage.error(res.data.msg)
        }
      })
    }
  })
}
</script>

<template>
  <div class="login">
    <el-form class="login-form" ref="loginFormRef" :model="loginForm" :rules="loginRules">
      <h2 class="title"><img src="@/assets/logo/logo.png">自习室预约平台</h2>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" placeholder="账号" :prefix-icon="User" />
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" placeholder="密码" :prefix-icon="Lock"
          @keyup.enter.native="handleLogin" />
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2025 fmboy.com 版权所有。</span>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-image: url('@/assets/images/login-background.jpg');
  background-size: cover;

  .login-form {
    margin: 20px;
    padding: 25px 25px 5px 25px;
    background: #ffffff90;
    width: 400px;
    border-radius: 6px;
    z-index: 1;

    .title {
      display: flex;
      align-items: center;
      justify-content: center;
      margin: 0 0 20px 0;
      color: #707070;
      font-size: 22px;

      img {
        width: 28px;
      }
    }

    .el-input {
      height: 38px;
      border-radius: 50px;
    }

    .el-button {
      margin-top: 10px;
      height: 35px;
      border-radius: 50px;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
}
</style>
